<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta name="apple-mobile-web-app-status-bar-style" content="black" />
	<meta name="format-detection" content="telephone=no"/>
	<title>信息填写</title>
	<link rel="stylesheet" href="css/lt_common.css">
	<link rel="stylesheet" href="css/write_information.css">
</head>
<body>
	<div class="page">
		<form action="/" id="inforform">
			<input name="id" id="id" type="hidden"/>
			<input name="qrcode" id="qrcode" type="hidden"/>
			<div class="main_infor">
				<!--
				<img src="img/default.png" alt="">
				<h3>王冲</h3>
				-->
			</div>
			<p class="requier">必填项</p>
			<div class="line_infor">
				<ul>
					<i class="tel_icon"></i>
					<li>
						电话
						<input id="phonenumber" name="phone" type="number" placeholder="请添加手机号码" value=""/>	
					</li>
					<i class="object_infor clear"></i>
					<li class="object">
						专业领域<em>（可多选）</em>	
					</li>
					<ul class="checkbox_content clear">
						<li>
							<label  for="acceident">
								<input onclick="selectFun($(this))" name="professional" id="acceident" type="checkbox" value="意外保险" />
								<i class="check"></i>意外保险
							</label>
						</li>
						<li>
							<label for="medical">
								<input onclick="selectFun($(this))" name="professional" id="medical" type="checkbox" value="医疗保险"/>
								<i class="check"></i>医疗保险
							</label>
						</li>			
						<li>
							<label for="juvenile">
								<input onclick="selectFun($(this))" name="professional" id="juvenile" type="checkbox" value="少儿保险"/>
								<i class="check"></i>少儿保险
							</label>
						</li>
						<li>
							<label for="endowment">
								<input onclick="selectFun($(this))" name="professional" id="endowment" type="checkbox" value="养老保险"/>
								<i class="check"></i>养老保险
							</label>
						</li>
						<li>
							<label for="life_insurance">
								<input onclick="selectFun($(this))" name="professional" id="life_insurance" type="checkbox" value="人寿保险"/>
								<i class="check"></i>人寿保险
							</label>
						</li>
						<li>
							<label for="finance">
								<input onclick="selectFun($(this))" name="professional" id="finance" type="checkbox" value="理财保险" />
								<i class="check"></i>理财保险
							</label>
						</li>
					</ul>
				</ul>
			</div>
			<p class="choose clear">选填项</p>
			<ul class="inline_way clear">
				<li class="add_line">
					<i class="add_icon"></i>
					添加社交信息<em>(QQ、微信、陌陌、邮箱)</em>	
				</li>
			</ul>	
			<ul class="honor clear">
				<li class="add_li">
					<i class="add_icon"></i>
					添加个人荣誉	
				</li>
			</ul>	
			<div class="save_button clear">
				<input type="button" value="保存">
			</div>	
		</div>
		<div class="line_div">
			<ul>
				<li><i class="qq"></i>QQ&nbsp;&nbsp;</li>
				<li><i class="weixin"></i>微信</li>
				<li><i class="momo"></i>陌陌</li>
				<li><i class="mail"></i>邮箱</li>
			</ul>
		</div>
	</form>
	<div class="save_mask">
		<img src="img/hint.png" alt="">
	</div>
</body>
</html>
<script src="js/jquery.js"></script>
<script src="js/common.js"></script>
<script src="js/write_information.js"></script>
<script>
	var inline_way_length=0;
	//模拟复选框
	function selectFun(obj){
		if ($(obj).siblings('i').attr('class') == 'check checkon') {
			$(obj).siblings('i').removeClass('checkon');
			$(obj).parents('label').css('color','#c7c7c7');
		}else {
			$(obj).siblings('i').addClass('checkon');
			$(obj).parents('label').css('color','#2b2b2b');
		}	
	}
	//该页面的监听
	$(function(){
		
		//初始化页面信息
		initdataAction();

		//添加社交信息
		$('.add_line').on('click',function(){
			$('.line_div').fadeIn('fast',function(){
				$('.line_div ul').slideDown('fast');
			});
		});
		$('.line_div').on('click',function(){
			$('.line_div').fadeOut('fast',function(){
				$('.line_div ul').slideUp('fast');
			});
		})
		$('.line_div ul li').on('click',function(){
			$('.line_div').fadeOut('fast',function(){
				$('.line_div ul').slideUp('fast');
			});
			var add_title = $(this).text();
			var li_obj = $('.inline_way li');
			inline_way_length ++;
			//区分是添加了什么选项
			var class_title = $(this).find('i').attr('class');
			var key = '';
			if(class_title == "qq"){
				key = 'qq';
			}
			if(class_title == "weixin"){
				key = 'wechat';
			}
			if(class_title == "momo"){
				key = 'momo';
			}
			if(class_title == "mail"){
				key = 'email';
			}
			//因为后台采用对象接受，所以需要知道是第几个变量所以需要赋下标值
			$('.inline_way').prepend('<li><i class="remove" onclick="$(this).parents(\'li\').remove()"></i>'+add_title+'<input type="text"  name=\"agentsocial['+inline_way_length+'].social_value\" value=""><input type="hidden"  name=\"agentsocial['+inline_way_length+'].social_key\" value=\"'+key+'\"></li>');

		});
		//添加荣誉
		$('.add_li').on('click',function(){
			$('.honor').prepend('<li><i class="remove" onclick="$(this).parents(\'li\').remove()" ></i><em class="add_title">荣誉</em><textarea name="agent_honor"></textarea></li>');
		});
		//保存提示
		$('.save_button').on('click',function(){
			if($('#phonenumber').val() == '' || $('.checkbox_content li label i').hasClass('checkon') == false ){
				$('body').scrollTop('0');
				$('.save_mask').fadeIn('fast');
			}else if($('#phonenumber').val().length >11 || $('#phonenumber').val().length < 11 ){
				alert('手机号必须是十一位数字哦！');
			}else {
					//异步提交表单
					$.ajax({
						url: API_URL+'agent/agentupdate',
						type: 'POST',
						dataType: "JSON",
						data: $("#inforform").serialize(),
						async: false,
						success: function (obj){
							if (obj.status.code == 0) {
								alert('保存成功');
								window.location = 'main_card.html?agent_status=1&origopenid='+origopenid;
							}
						},
						error: function (data){
							console.log("服务器异常!");
						}
					});
			
				
			}; 
		});
		$('.save_mask').on('click',function(){
			$('.save_mask').fadeOut('fast');
		})

	});
</script>
<script>

	//var openid = getQueryString('openid');

	//初始化
	function initdataAction(){
		$.ajax({
			url: API_URL+'agent/getbyopenid?openid='+origopenid,
			type: 'GET',
			dataType: "JSON",
			data:'',
			async: false,
			success: function (obj){
				if (obj.status.code == 0) {
					var myData = obj.data;
					for(var i=0; i < myData.length; i++){
						var agentItem = myData[i];
						//主键ID
						$('#id').val(agentItem.id);
						//该代理人是否生成了二维码参数
						$('#qrcode').val(agentItem.qrcode);
						if(agentItem.icon == null || agentItem.icon == ''){
							agentItem.icon ='img/default.png';
						}
						//名称与头像赋值
						var Str = '<img src=\"'+agentItem.icon+'\" alt=""><h3>'+agentItem.name+'</h3>';
						$('.main_infor').html(Str);
						//电话赋值
						if(agentItem.phone !=null && agentItem.phone != ""){
							$('#phonenumber').val(agentItem.phone);
						}
						//专业领域
						var agent_porfess = agentItem.professional_str;
						var agent_input_porfess = $("input[name=professional]");
					
						if(agent_porfess!=null && agent_porfess != ""){
							for(var j = 0 ; j < agent_input_porfess.length ; j ++ ){
								var temp_input_porfess = agent_input_porfess[j];
								for(var k = 0 ; k < agent_porfess.length; k ++){
									if(temp_input_porfess.value == agent_porfess[k]){
										temp_input_porfess.checked = "checked";
										selectFun(temp_input_porfess);
									}
								}
							}
						}
						//添加社区信息
						if(agentItem.agentsocial != null && agentItem.agentsocial != ""){
							addcontact_item(agentItem.agentsocial);
						}
						//添加个人荣誉
						if(agentItem.agent_honor!= null && agentItem.agent_honor != ""){
							addhonor_item(agentItem.agent_honor);
						}
					}
				}else{
					console.log(obj.status.msg);
				}
			},
			error: function (data){
				console.log("服务器异常!");
			}
		});
	}

	//统一遍历追加 社区信息的方法
	function addcontact_item(item){
		inline_way_length = item.length;
		//social_key	代表是社交工具代号名称
		//social_value  代表社交工具,具体号码
		var key = '';
		var title = '';
		var value = '';
		for(var i = 0 ; i < item.length ; i ++){

			key = item[i].social_key;
			value = item[i].social_value;
			
			if(key!=null){
				if(key == "qq"){
					title = 'QQ&nbsp;&nbsp;';
				}
				if(key == "wechat"){
					title = '微信';
				}
				if(key == "momo"){
					title = '陌陌';
				}
				if(key == "email"){
					title = '邮箱';
				}

				$('.inline_way').prepend('<li><i class="remove" onclick="$(this).parents(\'li\').remove()" ></i>'+title+'<input type="text"  name=\"agentsocial['+i+'].social_value\" value=\"'+value+'\"><input type="hidden"  name=\"agentsocial['+i+'].social_key\" value=\"'+key+'\"></li>');
			}
		}
	}

	function addhonor_item(item){

		for(var i = 0 ; i < item.length ; i ++){
			$('.honor').prepend('<li><i class="remove" onclick="$(this).parents(\'li\').remove()" ></i><em class="add_title">荣誉</em><textarea name="agent_honor" >'+item[i]+'</textarea></li>');
		}
		
	}
</script>